<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <meta name="description" content="SHELL 脚本"/>
    <meta name="keywords"
          content="nginx,freebsd,linux,mysql,shell,apache,centos,redhat,rhel,windows,sql,系统运维,IT运维,开源系统,开源系统运维"/>
    <title> SHELL 脚本 </title>

</head>
<body bgcolor="#FFFFFF" text="#000000">
<div id="menu">
    <ul>
		<span style="FONT-SIZE: 10.5pt">

			<li><a href="index.htm" hidefocus="true" id="mn_list">linux入门教程</a></li>

		</span>
    </ul>
</div>
<p style="text-align: center;"><span style="font-family:'宋体';font-size:15pt;"><b>第十四章</b></span><span
        style="font-family:'Calibri';font-size:15pt;"><b>  SHELL </b></span><span
        style="font-family:'宋体';font-size:15pt;"><b>脚本</b></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">	</span><span
        style="font-family:'宋体';font-size:10.5pt;">终于到</span><span
        style="font-family:'Calibri';font-size:10.5pt;">shell </span><span style="font-family:'宋体';font-size:10.5pt;">脚本这章了，在以前笔者卖了好多关子说</span><span
        style="font-family:'Calibri';font-size:10.5pt;">shell</span><span style="font-family:'宋体';font-size:10.5pt;">脚本怎么怎么重要，确实</span><span
        style="font-family:'Calibri';font-size:10.5pt;">shell</span><span
        style="font-family:'宋体';font-size:10.5pt;">脚本在</span><span
        style="font-family:'Calibri';font-size:10.5pt;">linux</span><span style="font-family:'宋体';font-size:10.5pt;">系统管理员的运维工作中非常非常重要</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">下面笔者就带你正式进入</span><span
        style="font-family:'Calibri';font-size:10.5pt;">shell</span><span style="font-family:'宋体';font-size:10.5pt;">脚本的世界吧</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>


<p><span style="font-family:'Calibri';font-size:10.5pt;">	</span><span style="font-family:'宋体';font-size:10.5pt;">到现在为止，你明白什么是</span><span
        style="font-family:'Calibri';font-size:10.5pt;">shell</span><span style="font-family:'宋体';font-size:10.5pt;">脚本吗？如果明白最好了，不明白也没有关系，相信随着学习的深入你就会越来越了解到底什么是</span><span
        style="font-family:'Calibri';font-size:10.5pt;">shell</span><span
        style="font-family:'宋体';font-size:10.5pt;">脚本</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">首先它是一个脚本，并不能作为正式的编程语言</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span
        style="font-family:'宋体';font-size:10.5pt;">因为是跑在</span><span style="font-family:'Calibri';font-size:10.5pt;">linux</span><span
        style="font-family:'宋体';font-size:10.5pt;">的</span><span
        style="font-family:'Calibri';font-size:10.5pt;">shell</span><span style="font-family:'宋体';font-size:10.5pt;">中，所以叫</span><span
        style="font-family:'Calibri';font-size:10.5pt;">shell</span><span
        style="font-family:'宋体';font-size:10.5pt;">脚本</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span
        style="font-family:'宋体';font-size:10.5pt;">说白了，</span><span style="font-family:'Calibri';font-size:10.5pt;">shell</span><span
        style="font-family:'宋体';font-size:10.5pt;">脚本就是一些命令的集合</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">举个例子，我想实现这样的操作：</span><span
        style="font-family:'Calibri';font-size:10.5pt;">1</span><span
        style="font-family:'宋体';font-size:10.5pt;">）进入到</span><span style="font-family:'Calibri';font-size:10.5pt;">/tmp/</span><span
        style="font-family:'宋体';font-size:10.5pt;">目录；</span><span
        style="font-family:'Calibri';font-size:10.5pt;">2</span><span style="font-family:'宋体';font-size:10.5pt;">）列出当前目录中所有的文件名；</span><span
        style="font-family:'Calibri';font-size:10.5pt;">3</span><span style="font-family:'宋体';font-size:10.5pt;">）把所有当前的文件拷贝到</span><span
        style="font-family:'Calibri';font-size:10.5pt;">/root/</span><span style="font-family:'宋体';font-size:10.5pt;">目录下；</span><span
        style="font-family:'Calibri';font-size:10.5pt;">4</span><span style="font-family:'宋体';font-size:10.5pt;">）删除当前目录下所有的文件</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span
        style="font-family:'宋体';font-size:10.5pt;">简单的</span><span
        style="font-family:'Calibri';font-size:10.5pt;">4</span><span
        style="font-family:'宋体';font-size:10.5pt;">步在</span><span
        style="font-family:'Calibri';font-size:10.5pt;">shell</span><span style="font-family:'宋体';font-size:10.5pt;">窗口中需要你敲</span><span
        style="font-family:'Calibri';font-size:10.5pt;">4</span><span
        style="font-family:'宋体';font-size:10.5pt;">次命令，按</span><span
        style="font-family:'Calibri';font-size:10.5pt;">4</span><span
        style="font-family:'宋体';font-size:10.5pt;">次回车</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">这样是不是很麻烦？当然这</span><span
        style="font-family:'Calibri';font-size:10.5pt;">4</span><span style="font-family:'宋体';font-size:10.5pt;">步操作非常简单，如果是更加复杂的命令设置需要几十次操作呢？那样的话一次一次敲键盘会很麻烦</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">所以不妨把所有的操作都记录到一个文档中，然后去调用文档中的命令，这样一步操作就可以完成</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">其实这个文档呢就是</span><span
        style="font-family:'Calibri';font-size:10.5pt;">shell</span><span style="font-family:'宋体';font-size:10.5pt;">脚本了，只是这个</span><span
        style="font-family:'Calibri';font-size:10.5pt;">shell</span><span style="font-family:'宋体';font-size:10.5pt;">脚本有它特殊的格式</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">	Shell</span><span
        style="font-family:'宋体';font-size:10.5pt;">脚本能帮助我们很方便的去管理服务器，因为我们可以指定一个任务计划定时去执行某一个</span><span
        style="font-family:'Calibri';font-size:10.5pt;">shell</span><span style="font-family:'宋体';font-size:10.5pt;">脚本实现我们想要需求</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span
        style="font-family:'宋体';font-size:10.5pt;">这对于</span><span
        style="font-family:'Calibri';font-size:10.5pt;">linux</span><span style="font-family:'宋体';font-size:10.5pt;">系统管理员来说是一件非常值得自豪的事情</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span
        style="font-family:'宋体';font-size:10.5pt;">现在的</span><span
        style="font-family:'Calibri';font-size:10.5pt;">139</span><span style="font-family:'宋体';font-size:10.5pt;">邮箱很好用，发邮件的同时还可以发一条邮件通知的短信给用户，利用这点，我们就可以在我们的</span><span
        style="font-family:'Calibri';font-size:10.5pt;">linux</span><span style="font-family:'宋体';font-size:10.5pt;">服务器上部署监控的</span><span
        style="font-family:'Calibri';font-size:10.5pt;">shell</span><span style="font-family:'宋体';font-size:10.5pt;">脚本，比如网卡流量有异常了或者服务器</span><span
        style="font-family:'Calibri';font-size:10.5pt;">web</span><span style="font-family:'宋体';font-size:10.5pt;">服务器停止了就可以发一封邮件给管理员，同时发送给管理员一个报警短信这样可以让我们及时的知道服务器出问题了</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">	</span><span style="font-family:'宋体';font-size:10.5pt;">有一个问题需要约定一下，</span><span
        style="font-family:'宋体';font-size:10.5pt; color:#FF0000;">凡是自定义的脚本建议放到</span><span
        style="font-family:'Calibri';font-size:10.5pt; color:#FF0000;">/usr/local/sbin/</span><span
        style="font-family:'宋体';font-size:10.5pt; color:#FF0000;">目录下</span><span
        style="font-family:'宋体';font-size:10.5pt;">，这样做的目的是，一来可以更好的管理文档；二来以后接管你的管理员都知道自定义脚本放在哪里，方便维护</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">【</span><span
        style="font-family:'Calibri';font-size:10.5pt;"><b>shell</b></span><span
        style="font-family:'宋体';font-size:10.5pt;"><b>脚本的基本结构以及如何执行</b></span><span
        style="font-family:'Calibri';font-size:10.5pt;">】</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="14_1.png"></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">Shell</span><span style="font-family:'宋体';font-size:10.5pt;">脚本通常都是以</span><span
        style="font-family:'Calibri';font-size:10.5pt;">.sh </span><span style="font-family:'宋体';font-size:10.5pt;">为后缀名的，这个并不是说不带</span><span
        style="font-family:'Calibri';font-size:10.5pt;">.sh</span><span style="font-family:'宋体';font-size:10.5pt;">这个脚本就不能执行，只是大家的一个习惯而已</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">所以，以后你发现了</span><span
        style="font-family:'Calibri';font-size:10.5pt;">.sh</span><span style="font-family:'宋体';font-size:10.5pt;">为后缀的文件那么它一定会是一个</span><span
        style="font-family:'Calibri';font-size:10.5pt;">shell</span><span
        style="font-family:'宋体';font-size:10.5pt;">脚本了</span><span style="font-family:'Calibri';font-size:10.5pt;">。test.sh</span><span
        style="font-family:'宋体';font-size:10.5pt;">中第一行一定是</span><span style="font-family:'Calibri';font-size:10.5pt;"> “#! /bin/bash” </span><span
        style="font-family:'宋体';font-size:10.5pt;">它代表的意思是，该文件使用的是</span><span
        style="font-family:'Calibri';font-size:10.5pt;">bash</span><span
        style="font-family:'宋体';font-size:10.5pt;">语法</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">如果不设置该行，那么你的</span><span
        style="font-family:'Calibri';font-size:10.5pt;">shell</span><span style="font-family:'宋体';font-size:10.5pt;">脚本就不能被执行</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。’#’</span><span style="font-family:'宋体';font-size:10.5pt;">表示注释，在前面讲过的</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">后面跟一些该脚本的相关注释内容以及作者和创建日期或者版本等等</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">当然这些注释并非必须的，如果你懒的很，可以省略掉，但是笔者不建议省略</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">因为随着你工作时间的增加，你写的</span><span
        style="font-family:'Calibri';font-size:10.5pt;">shell</span><span style="font-family:'宋体';font-size:10.5pt;">脚本也会越来越多，如果有一天你回头查看你写的某个脚本时，很有可能忘记该脚本是用来干什么的以及什么时候写的</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">所以写上注释是有必要的</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">另外系统管理员并非你一个，如果是其他管理员查看你的脚本，他看不懂岂不是很郁闷</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">该脚本再往下面则为要运行的命令了</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="14_7.png"></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">Shell</span><span style="font-family:'宋体';font-size:10.5pt;">脚本的执行很简单，直接</span><span
        style="font-family:'Calibri';font-size:10.5pt;">”sh  filename “ </span><span
        style="font-family:'宋体';font-size:10.5pt;">即可，另外你还可以这样执行</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="14_8.png"></span></p>
<p><span style="font-family:'宋体';font-size:10.5pt;">默认我们用</span><span style="font-family:'Calibri';font-size:10.5pt;">vim</span><span
        style="font-family:'宋体';font-size:10.5pt;">编辑的文档是不带有执行权限的，所以需要加一个执行权限，那样就可以直接使用</span><span
        style="font-family:'Calibri';font-size:10.5pt;">’./filename’ </span><span
        style="font-family:'宋体';font-size:10.5pt;">执行这个脚本了</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span
        style="font-family:'宋体';font-size:10.5pt;">另外使用</span><span
        style="font-family:'Calibri';font-size:10.5pt;">sh</span><span style="font-family:'宋体';font-size:10.5pt;">命令去执行一个</span><span
        style="font-family:'Calibri';font-size:10.5pt;">shell</span><span style="font-family:'宋体';font-size:10.5pt;">脚本的时候是可以加</span><span
        style="font-family:'Calibri';font-size:10.5pt;">-x</span><span style="font-family:'宋体';font-size:10.5pt;">选项来查看这个脚本执行过程的，这样有利于我们调试这个脚本哪里出了问题</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="14_9.png"></span></p>
<p><span style="font-family:'宋体';font-size:10.5pt;">该</span><span
        style="font-family:'Calibri';font-size:10.5pt;">shell</span><span style="font-family:'宋体';font-size:10.5pt;">脚本中用到了</span><span
        style="font-family:'Calibri';font-size:10.5pt;">’date’</span><span style="font-family:'宋体';font-size:10.5pt;">这个命令，它的作用就是用来打印当前系统的时间</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span
        style="font-family:'宋体';font-size:10.5pt;">其实在</span><span
        style="font-family:'Calibri';font-size:10.5pt;">shell</span><span
        style="font-family:'宋体';font-size:10.5pt;">脚本中</span><span
        style="font-family:'Calibri';font-size:10.5pt;">date</span><span style="font-family:'宋体';font-size:10.5pt;">使用率非常高</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">有几个选项笔者常常在</span><span
        style="font-family:'Calibri';font-size:10.5pt;">shell</span><span style="font-family:'宋体';font-size:10.5pt;">脚本中用到：</span>
</p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="14_10.png"></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">%Y</span><span
        style="font-family:'宋体';font-size:10.5pt;">表示年，</span><span
        style="font-family:'Calibri';font-size:10.5pt;">%m</span><span
        style="font-family:'宋体';font-size:10.5pt;">表示月，</span><span
        style="font-family:'Calibri';font-size:10.5pt;">%d</span><span
        style="font-family:'宋体';font-size:10.5pt;">表示日期，</span><span
        style="font-family:'Calibri';font-size:10.5pt;">%H</span><span
        style="font-family:'宋体';font-size:10.5pt;">表示小时，</span><span
        style="font-family:'Calibri';font-size:10.5pt;">%M</span><span
        style="font-family:'宋体';font-size:10.5pt;">表示分钟，</span><span
        style="font-family:'Calibri';font-size:10.5pt;">%S</span><span
        style="font-family:'宋体';font-size:10.5pt;">表示秒</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="14_11.png"></span></p>
<p><span style="font-family:'宋体';font-size:10.5pt;">注意</span><span
        style="font-family:'Calibri';font-size:10.5pt;">%y</span><span
        style="font-family:'宋体';font-size:10.5pt;">和</span><span
        style="font-family:'Calibri';font-size:10.5pt;">%Y</span><span
        style="font-family:'宋体';font-size:10.5pt;">的区别</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="14_21.png"></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">-d </span><span style="font-family:'宋体';font-size:10.5pt;">选项也是经常要用到的，它可以打印</span><span
        style="font-family:'Calibri';font-size:10.5pt;">n</span><span
        style="font-family:'宋体';font-size:10.5pt;">天前或者</span><span
        style="font-family:'Calibri';font-size:10.5pt;">n</span><span style="font-family:'宋体';font-size:10.5pt;">天后的日期，当然也可以打印</span><span
        style="font-family:'Calibri';font-size:10.5pt;">n</span><span
        style="font-family:'宋体';font-size:10.5pt;">个月</span><span
        style="font-family:'Calibri';font-size:10.5pt;">/</span><span style="font-family:'宋体';font-size:10.5pt;">年前或者后的日期</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。 </span></p>
<p><img src="14_22.png">另外星期几也是常用的</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="14_23.png"></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><b>【shell</b></span><span
        style="font-family:'宋体';font-size:10.5pt;"><b>脚本中的变量</b></span><span
        style="font-family:'Calibri';font-size:10.5pt;"><b>】</b></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">	</span><span
        style="font-family:'宋体';font-size:10.5pt;">在</span><span
        style="font-family:'Calibri';font-size:10.5pt;">shell</span><span style="font-family:'宋体';font-size:10.5pt;">脚本中使用变量显得我们的脚本更加专业更像是一门语言，开个玩笑，变量的作用当然不是为了专业</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">如果你写了一个长达</span><span
        style="font-family:'Calibri';font-size:10.5pt;">1000</span><span
        style="font-family:'宋体';font-size:10.5pt;">行的</span><span
        style="font-family:'Calibri';font-size:10.5pt;">shell</span><span style="font-family:'宋体';font-size:10.5pt;">脚本，并且脚本中出现了某一个命令或者路径几百次</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">突然你觉得路径不对想换一下，那岂不是要更改几百次？你固然可以使用批量替换的命令，但是也是很麻烦，并且脚本显得臃肿了很多</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">变量的作用就是用来解决这个问题的</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="14_24.png"></span></p>
<p><span style="font-family:'宋体';font-size:10.5pt;">在</span><span style="font-family:'Calibri';font-size:10.5pt;">test2.sh</span><span
        style="font-family:'宋体';font-size:10.5pt;">中使用到了反引号，你是否还记得它的作用？</span><span
        style="font-family:'Calibri';font-size:10.5pt;">’d’</span><span
        style="font-family:'宋体';font-size:10.5pt;">和</span><span
        style="font-family:'Calibri';font-size:10.5pt;">’d1’</span><span style="font-family:'宋体';font-size:10.5pt;">在脚本中作为变量出现，定义变量的格式为</span><span
        style="font-family:'Calibri';font-size:10.5pt;"> “</span><span
        style="font-family:'宋体';font-size:10.5pt;">变量名</span><span
        style="font-family:'Calibri';font-size:10.5pt;">=</span><span
        style="font-family:'宋体';font-size:10.5pt;">变量的值</span><span
        style="font-family:'Calibri';font-size:10.5pt;">”。</span><span style="font-family:'宋体';font-size:10.5pt;">当在脚本中引用变量时需要加上</span><span
        style="font-family:'Calibri';font-size:10.5pt;">’$’</span><span style="font-family:'宋体';font-size:10.5pt;">符号，这跟前面讲的在</span><span
        style="font-family:'Calibri';font-size:10.5pt;">shell</span><span style="font-family:'宋体';font-size:10.5pt;">中自定义变量是一致的</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">下面看看脚本执行结果吧</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="14_25.png"></span></p>
<p><span style="font-family:'宋体';font-size:10.5pt;">下面我们用</span><span style="font-family:'Calibri';font-size:10.5pt;">shell</span><span
        style="font-family:'宋体';font-size:10.5pt;">计算两个数的和</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="14_26.png"></span></p>
<p><span style="font-family:'宋体';font-size:10.5pt;">数学计算要用</span><span style="font-family:'Calibri';font-size:10.5pt;">’[ ]’</span><span
        style="font-family:'宋体';font-size:10.5pt;">括起来并且外头要带一个</span><span
        style="font-family:'Calibri';font-size:10.5pt;">’$’。</span><span style="font-family:'宋体';font-size:10.5pt;">脚本结果为：</span>
</p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="14_27.png"></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">Shell</span><span style="font-family:'宋体';font-size:10.5pt;">脚本还可以和用户交互</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="14_28.png"></span></p>
<p><span style="font-family:'宋体';font-size:10.5pt;">这就用到了</span><span style="font-family:'Calibri';font-size:10.5pt;">read</span><span
        style="font-family:'宋体';font-size:10.5pt;">命令了，它可以从标准输入获得变量的值，后跟变量名</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。”read  x”</span><span
        style="font-family:'宋体';font-size:10.5pt;">表示</span><span
        style="font-family:'Calibri';font-size:10.5pt;">x</span><span style="font-family:'宋体';font-size:10.5pt;">变量的值需要用户通过键盘输入得到</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">脚本执行过程如下：</span>
</p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="14_29.png"></span></p>
<p><span style="font-family:'宋体';font-size:10.5pt;">我们不妨加上</span><span style="font-family:'Calibri';font-size:10.5pt;">-x</span><span
        style="font-family:'宋体';font-size:10.5pt;">选项再来看看这个执行过程：</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="14_44.png"></span></p>
<p><span style="font-family:'宋体';font-size:10.5pt;">在</span><span style="font-family:'Calibri';font-size:10.5pt;">test4.sh</span><span
        style="font-family:'宋体';font-size:10.5pt;">中还有更加简洁的方式</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="14_45.png">执行如下：</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="14_46.png"></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">	</span><span style="font-family:'宋体';font-size:10.5pt;">你有没有用过这样的命令</span><span
        style="font-family:'Calibri';font-size:10.5pt;">”/etc/init.d/iptables restart “ </span><span
        style="font-family:'宋体';font-size:10.5pt;">前面的</span><span style="font-family:'Calibri';font-size:10.5pt;">/etc/init.d/iptables </span><span
        style="font-family:'宋体';font-size:10.5pt;">文件其实就是一个</span><span style="font-family:'Calibri';font-size:10.5pt;">shell</span><span
        style="font-family:'宋体';font-size:10.5pt;">脚本，为什么后面可以跟一个</span><span
        style="font-family:'Calibri';font-size:10.5pt;">”restart”? </span><span
        style="font-family:'宋体';font-size:10.5pt;">这里就涉及到了</span><span style="font-family:'Calibri';font-size:10.5pt;">shell</span><span
        style="font-family:'宋体';font-size:10.5pt;">脚本的预设变量</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span
        style="font-family:'宋体';font-size:10.5pt;">实际上，</span><span style="font-family:'Calibri';font-size:10.5pt;">shell</span><span
        style="font-family:'宋体';font-size:10.5pt;">脚本在执行的时候后边是可以跟变量的，而且还可以跟多个</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">不妨笔者写一个脚本，你就会明白了</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="14_47.png">执行过程如下：</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="14_48.png"></span></p>
<p><span style="font-family:'宋体';font-size:10.5pt;">在脚本中，你会不会奇怪，哪里来的</span><span
        style="font-family:'Calibri';font-size:10.5pt;">$1</span><span
        style="font-family:'宋体';font-size:10.5pt;">和</span><span
        style="font-family:'Calibri';font-size:10.5pt;">$2</span><span
        style="font-family:'宋体';font-size:10.5pt;">，这其实就是</span><span style="font-family:'Calibri';font-size:10.5pt;">shell</span><span
        style="font-family:'宋体';font-size:10.5pt;">脚本的预设变量，其中</span><span
        style="font-family:'Calibri';font-size:10.5pt;">$1</span><span style="font-family:'宋体';font-size:10.5pt;">的值就是在执行的时候输入的</span><span
        style="font-family:'Calibri';font-size:10.5pt;">1</span><span
        style="font-family:'宋体';font-size:10.5pt;">，而</span><span
        style="font-family:'Calibri';font-size:10.5pt;">$2</span><span style="font-family:'宋体';font-size:10.5pt;">的值就是执行的时候输入的</span><span
        style="font-family:'Calibri';font-size:10.5pt;">$2</span><span
        style="font-family:'宋体';font-size:10.5pt;">，当然一个</span><span style="font-family:'Calibri';font-size:10.5pt;">shell</span><span
        style="font-family:'宋体';font-size:10.5pt;">脚本的预设变量是没有限制的，这回你明白了吧</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span
        style="font-family:'宋体';font-size:10.5pt;">另外还有一个</span><span
        style="font-family:'Calibri';font-size:10.5pt;">$0</span><span style="font-family:'宋体';font-size:10.5pt;">，不过它代表的是脚本本身的名字</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">不妨把脚本修改一下</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="14_49.png">。</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="14_50.png"></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><b>【shell</b></span><span
        style="font-family:'宋体';font-size:10.5pt;"><b>脚本中的逻辑判断</b></span><span
        style="font-family:'Calibri';font-size:10.5pt;"><b>】</b></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">	</span><span style="font-family:'宋体';font-size:10.5pt;">如果你学过</span><span
        style="font-family:'Calibri';font-size:10.5pt;">C</span><span style="font-family:'宋体';font-size:10.5pt;">或者其他语言，相信你不会对</span><span
        style="font-family:'Calibri';font-size:10.5pt;">if </span><span
        style="font-family:'宋体';font-size:10.5pt;">陌生，在</span><span style="font-family:'Calibri';font-size:10.5pt;">shell</span><span
        style="font-family:'宋体';font-size:10.5pt;">脚本中我们同样可以使用</span><span
        style="font-family:'Calibri';font-size:10.5pt;">if</span><span
        style="font-family:'宋体';font-size:10.5pt;">逻辑判断</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span
        style="font-family:'宋体';font-size:10.5pt;">在</span><span
        style="font-family:'Calibri';font-size:10.5pt;">shell</span><span
        style="font-family:'宋体';font-size:10.5pt;">中</span><span
        style="font-family:'Calibri';font-size:10.5pt;">if</span><span style="font-family:'宋体';font-size:10.5pt;">判断的基本语法为：</span>
</p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><b>1</b></span><span
        style="font-family:'宋体';font-size:10.5pt;"><b>）不带</b></span><span
        style="font-family:'Calibri';font-size:10.5pt;"><b>else</b></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">	if  </span><span
        style="font-family:'宋体';font-size:10.5pt;">判断语句</span><span style="font-family:'Calibri';font-size:10.5pt;">; then</span>
</p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">		command</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">	fi</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="14_51.png"></span></p>
<p><span style="font-family:'宋体';font-size:10.5pt;">在</span><span
        style="font-family:'Calibri';font-size:10.5pt;">if1.sh</span><span style="font-family:'宋体';font-size:10.5pt;">中出现了</span><span
        style="font-family:'Calibri';font-size:10.5pt;"> ((a&lt;60))</span><span
        style="font-family:'宋体';font-size:10.5pt;">这样的形式，这是</span><span style="font-family:'Calibri';font-size:10.5pt;">shell</span><span
        style="font-family:'宋体';font-size:10.5pt;">脚本中特有的格式，用一个小括号或者不用都会报错，请记住这个格式，即可</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span
        style="font-family:'宋体';font-size:10.5pt;">执行结果为：</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="14_52.png"></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><b>2</b></span><span
        style="font-family:'宋体';font-size:10.5pt;"><b>）带有</b></span><span
        style="font-family:'Calibri';font-size:10.5pt;"><b>else</b></span></p>
<p style="text-indent: 21pt;"><span style="font-family:'Calibri';font-size:10.5pt;">if  </span><span
        style="font-family:'宋体';font-size:10.5pt;">判断语句</span><span style="font-family:'Calibri';font-size:10.5pt;">  ; then</span>
</p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">		command</span></p>
<p style="text-indent: 21pt;"><span style="font-family:'Calibri';font-size:10.5pt;">else </span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">		command</span></p>
<p style="text-indent: 21pt;"><span style="font-family:'Calibri';font-size:10.5pt;">fi</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="14_53.png"></span></p>
<p><span style="font-family:'宋体';font-size:10.5pt;">执行结果为：</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="14_54.png"></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><b>3</b></span><span
        style="font-family:'宋体';font-size:10.5pt;"><b>）带有</b></span><span
        style="font-family:'Calibri';font-size:10.5pt;"><b>elif</b></span></p>
<p style="text-indent: 21pt;"><span style="font-family:'Calibri';font-size:10.5pt;">if  </span><span
        style="font-family:'宋体';font-size:10.5pt;">判断语句一</span><span style="font-family:'Calibri';font-size:10.5pt;">  ; then</span>
</p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">		command</span></p>
<p style="text-indent: 21pt;"><span style="font-family:'Calibri';font-size:10.5pt;">elif  </span><span
        style="font-family:'宋体';font-size:10.5pt;">判断语句二</span><span style="font-family:'Calibri';font-size:10.5pt;">; then </span>
</p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">		command</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">	else</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">		command</span></p>
<p style="text-indent: 21pt;"><span style="font-family:'Calibri';font-size:10.5pt;">fi</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="14_55.png"></span></p>
<p><span style="font-family:'宋体';font-size:10.5pt;">这里的</span><span style="font-family:'Calibri';font-size:10.5pt;"> &amp;&amp; </span><span
        style="font-family:'宋体';font-size:10.5pt;">表示</span><span
        style="font-family:'Calibri';font-size:10.5pt;">“</span><span
        style="font-family:'宋体';font-size:10.5pt;">并且</span><span
        style="font-family:'Calibri';font-size:10.5pt;">”</span><span style="font-family:'宋体';font-size:10.5pt;">的意思，当然你也可以使用</span><span
        style="font-family:'Calibri';font-size:10.5pt;"> || </span><span
        style="font-family:'宋体';font-size:10.5pt;">表示</span><span
        style="font-family:'Calibri';font-size:10.5pt;">“</span><span
        style="font-family:'宋体';font-size:10.5pt;">或者</span><span
        style="font-family:'Calibri';font-size:10.5pt;">”</span><span
        style="font-family:'宋体';font-size:10.5pt;">，执行结果：</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="14_56.png"></span></p>
<p><span style="font-family:'宋体';font-size:10.5pt;">以上只是简单的介绍了</span><span
        style="font-family:'Calibri';font-size:10.5pt;">if</span><span
        style="font-family:'宋体';font-size:10.5pt;">语句的结构</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">在判断数值大小除了可以用</span><span
        style="font-family:'Calibri';font-size:10.5pt;">”(( ))”</span><span style="font-family:'宋体';font-size:10.5pt;">的形式外，还可以使用</span><span
        style="font-family:'Calibri';font-size:10.5pt;">”[ ]”。</span><span style="font-family:'宋体';font-size:10.5pt;">但是就不能使用</span><span
        style="font-family:'Calibri';font-size:10.5pt;">&gt;, &lt; , = </span><span
        style="font-family:'宋体';font-size:10.5pt;">这样的符号了，要使用</span><span
        style="font-family:'Calibri';font-size:10.5pt;"> -lt </span><span style="font-family:'宋体';font-size:10.5pt;">（小于），</span><span
        style="font-family:'Calibri';font-size:10.5pt;">-gt </span><span style="font-family:'宋体';font-size:10.5pt;">（大于），</span><span
        style="font-family:'Calibri';font-size:10.5pt;">-le </span><span style="font-family:'宋体';font-size:10.5pt;">（小于等于），</span><span
        style="font-family:'Calibri';font-size:10.5pt;">-ge </span><span style="font-family:'宋体';font-size:10.5pt;">（大于等于），</span><span
        style="font-family:'Calibri';font-size:10.5pt;">-eq </span><span style="font-family:'宋体';font-size:10.5pt;">（等于），</span><span
        style="font-family:'Calibri';font-size:10.5pt;">-ne </span><span style="font-family:'宋体';font-size:10.5pt;">（不等于）</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="14_57.png"></span></p>
<p><span style="font-family:'宋体';font-size:10.5pt;">再看看</span><span
        style="font-family:'Calibri';font-size:10.5pt;">if</span><span
        style="font-family:'宋体';font-size:10.5pt;">中使用</span><span style="font-family:'Calibri';font-size:10.5pt;"> &amp;&amp; </span><span
        style="font-family:'宋体';font-size:10.5pt;">和</span><span
        style="font-family:'Calibri';font-size:10.5pt;"> ||</span><span
        style="font-family:'宋体';font-size:10.5pt;">的情况</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="14_71.png"></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">shell </span><span style="font-family:'宋体';font-size:10.5pt;">脚本中</span><span
        style="font-family:'Calibri';font-size:10.5pt;">if</span><span style="font-family:'宋体';font-size:10.5pt;">还经常判断关于档案属性，比如判断是普通文件还是目录，判断文件是否有读写执行权限等</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">常用的也就几个选项：</span>
</p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">-e </span><span style="font-family:'宋体';font-size:10.5pt;">：判断文件或目录是否存在</span>
</p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">-d </span><span style="font-family:'宋体';font-size:10.5pt;">：判断是不是目录，并是否存在</span>
</p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">-f </span><span style="font-family:'宋体';font-size:10.5pt;">：判断是否是普通文件，并存在</span>
</p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">-r </span><span style="font-family:'宋体';font-size:10.5pt;">：判断文档是否有读权限</span>
</p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">-w </span><span style="font-family:'宋体';font-size:10.5pt;">：判断是否有写权限</span>
</p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">-x </span><span style="font-family:'宋体';font-size:10.5pt;">：判断是否可执行</span>
</p>
<p><span style="font-family:'宋体';font-size:10.5pt;">使用</span><span
        style="font-family:'Calibri';font-size:10.5pt;">if</span><span style="font-family:'宋体';font-size:10.5pt;">判断时，具体格式为：</span><span
        style="font-family:'Calibri';font-size:10.5pt;"> if [ -e filename ] ; then </span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="14_72.png"></span></p>
<p style="text-indent: 21pt;"><span style="font-family:'宋体';font-size:10.5pt;">在</span><span
        style="font-family:'Calibri';font-size:10.5pt;">shell </span><span style="font-family:'宋体';font-size:10.5pt;">脚本中，除了用</span><span
        style="font-family:'Calibri';font-size:10.5pt;">if</span><span style="font-family:'宋体';font-size:10.5pt;">来判断逻辑外，还有一种常用的方式，那就是</span><span
        style="font-family:'Calibri';font-size:10.5pt;">case</span><span
        style="font-family:'宋体';font-size:10.5pt;">了</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span
        style="font-family:'宋体';font-size:10.5pt;">具体格式为：</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">	case  </span><span
        style="font-family:'宋体';font-size:10.5pt;">变量</span><span
        style="font-family:'Calibri';font-size:10.5pt;">  in</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">	value1)</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">		command</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">		;;</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">	value2)</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">		command</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">		;;</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">	value3)</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">		command</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">		;;</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">	*)</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">		command</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">		;;</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">	esac</span></p>
<p><span style="font-family:'宋体';font-size:10.5pt;">上面的结构中，不限制</span><span
        style="font-family:'Calibri';font-size:10.5pt;">value</span><span style="font-family:'宋体';font-size:10.5pt;">的个数，</span><span
        style="font-family:'Calibri';font-size:10.5pt;">*</span><span style="font-family:'宋体';font-size:10.5pt;">则代表除了上面的</span><span
        style="font-family:'Calibri';font-size:10.5pt;">value</span><span style="font-family:'宋体';font-size:10.5pt;">外的其他值</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">下面笔者写一个判断输入数值是奇数或者偶数的脚本</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="14_73.png"></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">$a </span><span
        style="font-family:'宋体';font-size:10.5pt;">的值或为</span><span
        style="font-family:'Calibri';font-size:10.5pt;">1</span><span
        style="font-family:'宋体';font-size:10.5pt;">或为</span><span
        style="font-family:'Calibri';font-size:10.5pt;">0</span><span
        style="font-family:'宋体';font-size:10.5pt;">，执行结果为：</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="14_74.png"></span></p>
<p><span style="font-family:'宋体';font-size:10.5pt;">也可以看一下执行过程：</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="14_75.png"></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">case</span><span style="font-family:'宋体';font-size:10.5pt;">脚本常用于编写系统服务的启动脚本，例如</span><span
        style="font-family:'Calibri';font-size:10.5pt;">/etc/init.d/iptables</span><span
        style="font-family:'宋体';font-size:10.5pt;">中就用到了，你不妨去查看一下</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><b>【shell</b></span><span
        style="font-family:'宋体';font-size:10.5pt;"><b>脚本中的循环</b></span><span
        style="font-family:'Calibri';font-size:10.5pt;"><b>】</b></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">	Shell</span><span
        style="font-family:'宋体';font-size:10.5pt;">脚本中也算是一门简易的编程语言了，当然循环是不能缺少的</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span
        style="font-family:'宋体';font-size:10.5pt;">常用到的循环有</span><span style="font-family:'Calibri';font-size:10.5pt;">for</span><span
        style="font-family:'宋体';font-size:10.5pt;">循环和</span><span
        style="font-family:'Calibri';font-size:10.5pt;">while</span><span
        style="font-family:'宋体';font-size:10.5pt;">循环</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">下面就分别介绍一下两种循环的结构</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="14_76.png"></span></p>
<p><span style="font-family:'宋体';font-size:10.5pt;">脚本中的</span><span style="font-family:'Calibri';font-size:10.5pt;">seq 1 5 </span><span
        style="font-family:'宋体';font-size:10.5pt;">表示从</span><span
        style="font-family:'Calibri';font-size:10.5pt;">1</span><span
        style="font-family:'宋体';font-size:10.5pt;">到</span><span
        style="font-family:'Calibri';font-size:10.5pt;">5</span><span
        style="font-family:'宋体';font-size:10.5pt;">的一个序列</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">你可以直接运行这个命令试下</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">脚本执行结果为：</span>
</p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="14_77.png"></span></p>
<p><span style="font-family:'宋体';font-size:10.5pt;">通过这个脚本就可以看到</span><span
        style="font-family:'Calibri';font-size:10.5pt;">for</span><span style="font-family:'宋体';font-size:10.5pt;">循环的基本结构</span><span
        style="font-family:'Calibri';font-size:10.5pt;"> </span><span
        style="font-family:'宋体';font-size:10.5pt;">：</span></p>
<p style="text-indent: 21pt;"><span style="font-family:'Calibri';font-size:10.5pt;">for </span><span
        style="font-family:'宋体';font-size:10.5pt;">变量名</span><span
        style="font-family:'Calibri';font-size:10.5pt;"> in </span><span style="font-family:'宋体';font-size:10.5pt;">循环的条件；</span><span
        style="font-family:'Calibri';font-size:10.5pt;"> do</span></p>
<p style="text-indent: 21pt;"><span style="font-family:'Calibri';font-size:10.5pt;">	command</span></p>
<p style="text-indent: 21pt;"><span style="font-family:'Calibri';font-size:10.5pt;">done</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="14_78.png"></span></p>
<p><span style="font-family:'宋体';font-size:10.5pt;">循环的条件那一部分也可以写成这样的形式，中间用空格隔开即可</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span
        style="font-family:'宋体';font-size:10.5pt;">你也可以试试，</span><span style="font-family:'Calibri';font-size:10.5pt;">for i in `ls`; do echo $i; done </span><span
        style="font-family:'宋体';font-size:10.5pt;">和</span><span style="font-family:'Calibri';font-size:10.5pt;">  for i in `cat test.txt`</span><span
        style="font-family:'宋体';font-size:10.5pt;">；</span><span style="font-family:'Calibri';font-size:10.5pt;"> do echo $i; done</span>
</p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="14_79.png"></span></p>
<p><span style="font-family:'宋体';font-size:10.5pt;">再来看看这个</span><span style="font-family:'Calibri';font-size:10.5pt;">while</span><span
        style="font-family:'宋体';font-size:10.5pt;">循环，基本格式为：</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">	while  </span><span
        style="font-family:'宋体';font-size:10.5pt;">条件</span><span
        style="font-family:'Calibri';font-size:10.5pt;">; do</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">		command</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">	done</span></p>
<p><span style="font-family:'宋体';font-size:10.5pt;">脚本的执行结果为：</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="14_80.png"></span></p>
<p><span style="font-family:'宋体';font-size:10.5pt;">另外你可以把循环条件忽略掉，笔者常常这样写监控脚本</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p style="text-indent: 21pt;"><span style="font-family:'Calibri';font-size:10.5pt;">while :; do </span></p>
<p style="text-indent: 21pt;margin-left: 21pt;"><span style="font-family:'Calibri';font-size:10.5pt;">command </span>
</p>
<p style="text-indent: 21pt;"><span style="font-family:'Calibri';font-size:10.5pt;">done</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><b>【shell</b></span><span
        style="font-family:'宋体';font-size:10.5pt;"><b>脚本中的函数</b></span><span
        style="font-family:'Calibri';font-size:10.5pt;"><b>】</b></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">	</span><span style="font-family:'宋体';font-size:10.5pt;">如果你学过开发，肯定知道函数的作用</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">如果你是刚刚接触到这个概念的话，也没有关系，其实很好理解的</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">函数就是把一段代码整理到了一个小单元中，并给这个小单元起一个名字，当用到这段代码时直接调用这个小单元的名字即可</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">有时候脚本中的某段代总是重复使用，如果写成函数，每次用到时直接用函数名代替即可，这样就节省了时间还节省了空间</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="14_81.png"></span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">fun.sh </span><span style="font-family:'宋体';font-size:10.5pt;">中的</span><span
        style="font-family:'Calibri';font-size:10.5pt;">sum() </span><span style="font-family:'宋体';font-size:10.5pt;">为自定义的函数，在</span><span
        style="font-family:'Calibri';font-size:10.5pt;">shell</span><span style="font-family:'宋体';font-size:10.5pt;">脚本中要用</span><span
        style="font-family:'Calibri';font-size:10.5pt;"> </span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">function </span><span
        style="font-family:'宋体';font-size:10.5pt;">函数名</span><span
        style="font-family:'Calibri';font-size:10.5pt;">() { </span></p>
<p style="text-indent: 21pt;"><span style="font-family:'Calibri';font-size:10.5pt;">command </span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">}</span></p>
<p><span style="font-family:'宋体';font-size:10.5pt;">这样的格式去定义函数</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p><span style="font-family:'宋体';font-size:10.5pt;">上个脚本执行过程如下：</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;"><img src="14_82.png"></span></p>
<p><span style="font-family:'宋体';font-size:10.5pt;">有一点笔者要提醒你一下，在</span><span
        style="font-family:'Calibri';font-size:10.5pt;">shell</span><span style="font-family:'宋体';font-size:10.5pt;">脚本中，函数一定要写在最前面，不能出现在中间或者最后，因为函数是要被调用的，如果还没有出现就被调用，肯定是会出错的</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">	Shell</span><span
        style="font-family:'宋体';font-size:10.5pt;">脚本大体上就介绍这么多了，笔者所举的例子都是最基础的，所以即使你把所有例子完全掌握也不代表你的</span><span
        style="font-family:'Calibri';font-size:10.5pt;">shell</span><span style="font-family:'宋体';font-size:10.5pt;">脚本编写能力有多么好</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">所以剩下的日子里你尽量要多练习，多写脚本，你写的脚本越多，你的能力就越强</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">希望你能够找专门介绍</span><span
        style="font-family:'Calibri';font-size:10.5pt;">shell</span><span style="font-family:'宋体';font-size:10.5pt;">脚本的书籍深入的去研究一下它</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">随后笔者将给你留几个</span><span
        style="font-family:'Calibri';font-size:10.5pt;">shell</span><span style="font-family:'宋体';font-size:10.5pt;">脚本的练习题，你最好不要偷懒</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">1. </span><span
        style="font-family:'宋体';font-size:10.5pt;">编写</span><span
        style="font-family:'Calibri';font-size:10.5pt;">shell</span><span style="font-family:'宋体';font-size:10.5pt;">脚本，计算</span><span
        style="font-family:'Calibri';font-size:10.5pt;">1-100</span><span
        style="font-family:'宋体';font-size:10.5pt;">的和；</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">2. </span><span
        style="font-family:'宋体';font-size:10.5pt;">编写</span><span
        style="font-family:'Calibri';font-size:10.5pt;">shell</span><span style="font-family:'宋体';font-size:10.5pt;">脚本，要求输入一个数字，然后计算出从</span><span
        style="font-family:'Calibri';font-size:10.5pt;">1</span><span style="font-family:'宋体';font-size:10.5pt;">到输入数字的和，要求，如果输入的数字小于</span><span
        style="font-family:'Calibri';font-size:10.5pt;">1</span><span style="font-family:'宋体';font-size:10.5pt;">，则重新输入，直到输入正确的数字为止；</span>
</p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">3. </span><span
        style="font-family:'宋体';font-size:10.5pt;">编写</span><span
        style="font-family:'Calibri';font-size:10.5pt;">shell</span><span style="font-family:'宋体';font-size:10.5pt;">脚本，把</span><span
        style="font-family:'Calibri';font-size:10.5pt;">/root/</span><span style="font-family:'宋体';font-size:10.5pt;">目录下的所有目录（只需要一级）拷贝到</span><span
        style="font-family:'Calibri';font-size:10.5pt;">/tmp/</span><span style="font-family:'宋体';font-size:10.5pt;">目录下；</span>
</p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">4. </span><span
        style="font-family:'宋体';font-size:10.5pt;">编写</span><span
        style="font-family:'Calibri';font-size:10.5pt;">shell</span><span style="font-family:'宋体';font-size:10.5pt;">脚本，批量建立用户</span><span
        style="font-family:'Calibri';font-size:10.5pt;">user_00, user_01, … ,user_100</span><span
        style="font-family:'宋体';font-size:10.5pt;">并且所有用户同属于</span><span
        style="font-family:'Calibri';font-size:10.5pt;">users</span><span
        style="font-family:'宋体';font-size:10.5pt;">组；</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">5. </span><span
        style="font-family:'宋体';font-size:10.5pt;">编写</span><span
        style="font-family:'Calibri';font-size:10.5pt;">shell</span><span style="font-family:'宋体';font-size:10.5pt;">脚本，截取文件</span><span
        style="font-family:'Calibri';font-size:10.5pt;">test.log</span><span style="font-family:'宋体';font-size:10.5pt;">中包含关键词</span><span
        style="font-family:'Calibri';font-size:10.5pt;">’abc’</span><span style="font-family:'宋体';font-size:10.5pt;">的行中的第一列（假设分隔符为</span><span
        style="font-family:'Calibri';font-size:10.5pt;">”:”</span><span style="font-family:'宋体';font-size:10.5pt;">），然后把截取的数字排序（假设第一列为数字），然后打印出重复次数超过</span><span
        style="font-family:'Calibri';font-size:10.5pt;">10</span><span
        style="font-family:'宋体';font-size:10.5pt;">次的列；</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">6. </span><span
        style="font-family:'宋体';font-size:10.5pt;">编写</span><span
        style="font-family:'Calibri';font-size:10.5pt;">shell</span><span style="font-family:'宋体';font-size:10.5pt;">脚本，判断输入的</span><span
        style="font-family:'Calibri';font-size:10.5pt;">IP</span><span
        style="font-family:'宋体';font-size:10.5pt;">是否正确（</span><span
        style="font-family:'Calibri';font-size:10.5pt;">IP</span><span
        style="font-family:'宋体';font-size:10.5pt;">的规则是，</span><span style="font-family:'Calibri';font-size:10.5pt;">n1.n2.n3.n4</span><span
        style="font-family:'宋体';font-size:10.5pt;">，其中</span><span style="font-family:'Calibri';font-size:10.5pt;">1&lt;n1&lt;255, 0&lt;n2&lt;255, 0&lt;n3&lt;255, 0&lt;n4&lt;255</span><span
        style="font-family:'宋体';font-size:10.5pt;">）</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p><span style="font-family:'宋体';font-size:10.5pt;">以下为练习题答案：</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">1. #! /bin/bash</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">sum=0</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">for i in `seq 1 100`; do</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">        sum=$[$i+$sum]</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">done</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">echo $sum</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">2. #! /bin/bash</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">n=0</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">while [ $n -lt "1" ]; do</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">        read -p "Please input a number, it must greater than "1":" n</span>
</p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">done</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">&nbsp;</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">sum=0</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">for i in `seq 1 $n`; do</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">        sum=$[$i+$sum]</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">done</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">echo $sum</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">&nbsp;</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">3. #! /bin/bash</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">for f in `ls /root/`; do</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">        if [ -d $f ] ; then</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">                cp -r $f /tmp/</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">        fi</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">done</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">&nbsp;</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">4. #! /bin/bash</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">groupadd users</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">for i in `seq 0 9`; do</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">        useradd -g users user_0$i</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">done</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">&nbsp;</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">for j in `seq 10 100`; do</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">        useradd -g users user_$j</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">done</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">&nbsp;</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">5. #! /bin/bash</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">awk -F':' '$0~/abc/ {print $1}' test.log &gt;/tmp/n.txt</span>
</p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">sort -n n.txt |uniq -c |sort -n &gt;/tmp/n2.txt</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">awk '$1&gt;10 {print $2}' /tmp/n2.txt</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">&nbsp;</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">6. #! /bin/bash</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">checkip() {</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">        if echo $1 |egrep -q '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$' ; then</span>
</p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">                a=`echo $1 | awk -F. '{print $1}'`</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">                b=`echo $1 | awk -F. '{print $2}'`</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">                c=`echo $1 | awk -F. '{print $3}'`</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">                d=`echo $1 | awk -F. '{print $4}'`</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">&nbsp;</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">                for n in $a $b $c $d; do</span></p>
<p><span
        style="font-family:'Calibri';font-size:10.5pt;">                        if [ $n -ge 255 ] || [ $n -le 0 ]; then</span>
</p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">                                echo "the number of the IP should less than 255 and greate than 0"</span>
</p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">                                return 2</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">                        fi</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">                done</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">        else</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">                echo "The IP you input is something wrong, the format is like 192.168.100.1"</span>
</p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">                return 1</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">        fi</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">}</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">&nbsp;</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">rs=1</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">while [ $rs -gt 0 ]; do</span></p>
<p style="text-indent: 21pt;"><span
        style="font-family:'Calibri';font-size:10.5pt;">read -p  "Please input the ip:" ip</span></p>
<p style="text-indent: 21pt;"><span style="font-family:'Calibri';font-size:10.5pt;">checkip $ip</span></p>
<p style="text-indent: 21pt;"><span style="font-family:'Calibri';font-size:10.5pt;">rs=`echo $?`</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">done</span></p>
<p><span style="font-family:'Calibri';font-size:10.5pt;">echo "The IP is right!"</span></p>
<p style="text-align: justify;"><span style="font-family:'Calibri';font-size:10.5pt;">&nbsp;</span></p>

<p style="text-align: justify;"><span style="font-family:'宋体';font-size:10.5pt;">上一页 <a href="13.htm">正则表达式</a></span>
</p>
<p style="text-align: justify;"><span style="font-family:'宋体';font-size:10.5pt;">下一页 <a
        href="15.htm">linux系统日常管理</a></span></p>
<p style="text-align: justify;"><span style="font-family:'宋体';font-size:10.5pt;"><a
        href="index.htm">回到主目录</a>&nbsp;</span></p>

</body>
</html>